package com.github.jin.simpledropdownmenu

import android.animation.ObjectAnimator
import android.content.Context
import android.graphics.Canvas
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.PopupWindow
import com.hitomi.cslibrary.CrazyShadow
import com.hitomi.cslibrary.base.CrazyShadowDirection
import kotlinx.android.synthetic.main.down_menu_title.view.*
import kotlinx.android.synthetic.main.popup_menu.view.*
import org.jetbrains.anko.imageResource
import org.jetbrains.anko.onClick


/**
 * @author Jin .
 * creation date 2017/11/3 10:04.
 * modification date 2017/11/3 10:04.
 * describe
 */
class SimpleDropDownMenu(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0) :
        LinearLayout(context, attrs, defStyleAttr, defStyleRes),
        SimpleDropDownMenuAdapter.ItemClickListener {
    private var mMenuItemList = mutableListOf<MenuItem>()
    private var mMenuItemAdapter: SimpleDropDownMenuAdapter? = null
    private var mMenuOpenState = false
    var dropDownMenuOpenStateListener: OnDropDownMenuOpenStateListener? = null
    var menuItemClickListener: OnMenuItemClickListener? = null
    var clickItemCloseMenu = true
    private var mTitleItem: MenuItem? = null
    private lateinit var mRootView: View
    //弹出窗窗口
    private var mPopupWindow: PopupWindow? = null
    //阴影(PopupWindow 使用卡片式布局无法设置阴影? 采用其他的阴影添加方式)
    private var mShadow: CrazyShadow? = null

    interface OnDropDownMenuOpenStateListener {
        fun dropDownMenuStateChanges(menuOpenState: Boolean)
    }

    interface OnMenuItemClickListener {
        fun onMenuItemClickListener(position: Int)
    }

    constructor(mContext: Context, mAttrs: AttributeSet?) : this(context = mContext, attrs = mAttrs) {
        initView()
    }

    private fun initView() {
        mRootView = LayoutInflater.from(context).inflate(R.layout.down_menu_title, this)
        initPopWindows()
        mRootView.onClick {
            if (mMenuOpenState) {
                closeMenu()
            } else {
                openMenu()
            }
        }
    }

    private fun initPopWindows() {
        val popWindows = LayoutInflater.from(context).inflate(R.layout.popup_menu, null)
        mPopupWindow = PopupWindow(popWindows, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true)
        mPopupWindow?.animationStyle = R.style.MyPopupWindow_anim_style
        mPopupWindow?.setOnDismissListener {
            closeMenu()
        }
        mMenuItemAdapter = SimpleDropDownMenuAdapter(mMenuItemList)
        mMenuItemAdapter?.itemClickListener = this
        (popWindows.menu_item_list as RecyclerView).layoutManager = LinearLayoutManager(this.context, LinearLayoutManager.VERTICAL, false)
        (popWindows.menu_item_list as RecyclerView).adapter = mMenuItemAdapter
    }

    private fun openMenu() {
        menuOpenStateChange(true)
        startOpenMenuAnimator()
        mPopupWindow?.showAsDropDown(mRootView, 0, 0)
        showShadow()
    }


    private fun showShadow() {
        mShadow = CrazyShadow.Builder()
                .setContext(context)
                .setDirection(CrazyShadowDirection.ALL)
                .setShadowRadius(5f)
                .setCorner(10f)
                .setImpl(CrazyShadow.IMPL_WRAP)
                .action(mPopupWindow?.contentView?.popup_menu_linear)
    }

    private fun removeShadow() {
        mShadow?.remove()
    }

    private fun closeMenu() {
        menuOpenStateChange(false)
        startCloseMenuAnimator()
        mPopupWindow?.dismiss()
        removeShadow()
    }

    private fun menuOpenStateChange(state: Boolean) {
        mMenuOpenState = state
        dropDownMenuOpenStateListener?.dropDownMenuStateChanges(mMenuOpenState)
    }

    private fun startOpenMenuAnimator() {
        val oa = ObjectAnimator.ofFloat(mRootView.menu_img_up_down,
                "rotation", 0f, -230f, -180f)
        oa.duration = 300
        oa.start()
    }

    private fun startCloseMenuAnimator() {
        val oa = ObjectAnimator.ofFloat(mRootView.menu_img_up_down,
                "rotation", -180f, 50f, 0f)
        oa.duration = 300
        oa.start()
    }

    fun addMenuItem(menuItemList: MutableList<MenuItem>, titleItem: Int = 0) {
        this.mMenuItemList.clear()
        this.mMenuItemList.addAll(menuItemList)
        // 如果默认列表第一项
        setTitleItem(menuItemList[titleItem])
        mMenuItemList.remove(menuItemList[titleItem])
        this.mMenuItemAdapter?.notifyDataSetChanged()
    }

    // 设置标题项
    fun setTitleItem(titleItem: MenuItem) {
        this.mTitleItem = titleItem
        mRootView.down_menu_title_item.text = titleItem.itemTitle
        mRootView.menu_title_img_icon.imageResource = titleItem.itemIcon
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        if (mMenuOpenState) {
            mPopupWindow?.isTouchable = true
            mPopupWindow?.isOutsideTouchable = true
        }
    }

    override fun onItemClick(position: Int) {
        if (clickItemCloseMenu) {
            closeMenu()
        }
        menuItemClickListener?.onMenuItemClickListener(position)
    }
}
